package com.yt.system.controller;

import com.yt.system.domain.Role;
import com.yt.system.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
@CrossOrigin(allowCredentials = "true")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 权限分配
     * @param roid  角色id
     * @param permission  例：{"A":["A1"],"B":["B1","B2"]}
     * @return
     */
    @PostMapping("/permissionAssignment")
    @ApiOperation("权限分配；传入参数：roid，permission（例：{\"A\":[\"A1\"],\"B\":[\"B1\",\"B2\"]}")
    public String permissionAssignment(Integer roid,String permission){
        Role role = new Role();
        String menu = "";
        System.out.println("-------"+roid+" "+permission);
        String[] aa = permission.split(":");
        for (String permission1: aa) {
            String menu1 = permission1.substring(permission1.length()-2,permission1.length()-1);
            menu += menu1;
        }
        menu = "["+menu;
        System.out.println("-------------"+menu);
        Integer i = roleService.updateRolePemission(roid, menu, permission);
        return "权限分配成功";
    }

    /**
     * 导航栏下拉框
     */
    @PostMapping("/menuPermission")
    @ApiOperation("导航栏下拉框；传入参数：roid，str（例：A)")
    public String menuPermission(Integer roid,String str){
        System.out.println("--------"+roid+" "+str);
        String quStr = "菜单"+str+"没有子集";
        Role role = roleService.selectByPrimaryKey(roid);
        String pemission = role.getPermission();
        System.out.println("permission"+pemission);
        if (pemission.contains(str)){
            quStr=pemission.substring(pemission.indexOf(str)+3,pemission.lastIndexOf(str)+4);
        }
        System.out.println("----------quStr---------"+quStr);
        return quStr;
    }

    /**
     *  查询所有角色列表
     */
    @PostMapping("/selectAllRole")
    @ApiOperation("查询所有角色列表")
    public List<Role> selectAllRole(){
        List<Role> roles = roleService.selectAllRole();
        return roles;
    }

    /**
     * 修改角色信息
     */
    @PostMapping("/updateRole")
    @ApiOperation("修改角色信息；参数：roid，rolename，content")
    public String updateRole(Integer roid, String rolename, String content) {
        Date updatetime = new Date();
        int i = roleService.updateRole(roid, rolename, content, updatetime);
        if (i>0){
            return "success";
        }else {
            return "error";
        }
    }

    /**
     * 删除角色
     */
    @PostMapping("/deleteRole")
    @ApiOperation("删除角色信息；参数：roid")
    public String deleteRole(Integer roid) {
        int i = roleService.deleteByPrimaryKey(roid);
        if (i>0){
            return "success";
        }else {
            return "error";
        }
    }

    /**
     * 根据名称或者描述进行查询
     */
    @PostMapping("/selectLikeNameOrContent")
    @ApiOperation("根据名称或者描述进行查询；参数：str")
    public List<Role> selectLikeNameOrContent(String str) {
        List<Role> roles = roleService.selectLikeNameOrContent(str);
        return roles;
    }

    /**
     * 根据名称或者描述进行查询
     */
    @PostMapping("/insertRole")
    @ApiOperation("新增角色；参数：传入对象role")
    public String insertRole(Role role) {
        Date createTime = new Date();
        role.setCreatetime(createTime);
        int i = roleService.insertSelective(role);
        if (i>0){
            return "新增角色成功";
        }else {
            return "新增角色失败";
        }
    }

}
